#include "vas_rocketmq.h"
#include <rocketmq/DefaultMQProducer.h>

namespace vas {
    std::string ROCKET_MQ_HOST = "";
    rocketmq::DefaultMQProducer *producer = nullptr;

    void RocketMQInit(const std::string &hosts) {
        if (hosts.empty()) {
            //VAS_INFO("RocketMQ host is empty, init skipped.");
            return;
        }
        ROCKET_MQ_HOST = hosts;
        if (producer != nullptr) {
            //VAS_INFO("RocketMQ is initialized, stop and init again.");
            producer->shutdown();
            delete producer;
            producer = nullptr;
        }
        producer = new rocketmq::DefaultMQProducer(ROCKETMQ_PRODUCER_GROUP);
        producer->setNamesrvAddr(ROCKET_MQ_HOST);
        producer->start();
    }

    void RocketMQSend(const std::string &msg_str, const std::string &topic) {
        if (producer != nullptr) {
            rocketmq::MQMessage msg(topic, msg_str);
            producer->send(msg);
        } else {
            //VAS_INFO("RocketMQ is not initialized, send msg skipped.");
        }
    }

    void RocketMQStop() {
        if (producer != nullptr) {
            producer->shutdown();
        } else {
            //VAS_INFO("RocketMQ is not initialized, stop skipped.");
        }
    }
}

